package com.yunxi.controller.sto;

import com.aliyuncs.utils.StringUtils;
import com.yunxi.core.util.StringUtil;
import com.yunxi.core.web.ApiResult;
import com.yunxi.core.web.CurrentUser;
import com.yunxi.core.web.LoginInfo;
import com.yunxi.core.web.ResponseUtil;
import com.yunxi.dealer.pojo.PartnerVO;
import com.yunxi.sto.pojo.*;
import com.yunxi.sto.service.StoChangeStorageDubboService;
import io.swagger.annotations.*;
import org.apache.commons.beanutils.BeanUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;


/**
 * <p>
 * 车辆移库单 前端控制器
 * </p>
 *
 * @author caoYong
 * @since 2019-05-28
 */
@RestController
@RequestMapping("/admin/stoChangeStorage")
@Api(tags = "sto:changeStorage", description = "车辆移库单接口")
public class StoChangeStorageController {
    private static final Logger logger = LoggerFactory.getLogger(StoChangeStorageController.class);

    @Autowired
    StoChangeStorageDubboService stoChangeStorageDubboService;

    @GetMapping("/page")
    @ApiOperation(value = "查询分页")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "pageNum", value = "分页页码", required = true, dataType = "int"),
            @ApiImplicitParam(paramType = "query", name = "pageSize", value = "分页数量", required = true, dataType = "int")
    })
    public ApiResult page(StoChangeStorageQueryVO stoChangeStorageQueryVO, Integer pageNum, Integer pageSize) throws Exception {
     return ResponseUtil.ok(stoChangeStorageDubboService.findPageByVO(stoChangeStorageQueryVO, pageNum, pageSize));
    }

    @GetMapping("/load")
    @ApiOperation(value = "读取单个")
    public ApiResult load(StoChangeStorageVO stoChangeStorageVO, @CurrentUser LoginInfo loginInfo) throws Exception {
     return ResponseUtil.ok(stoChangeStorageDubboService.getById(stoChangeStorageVO.getChangeStorageId()));
    }

    @PostMapping("/save")
    @ApiOperation(value = "保存信息")
    public ApiResult save(@RequestBody StoChangeStorageEditVO stoChangeStorageEditVO, @CurrentUser LoginInfo loginInfo) throws Exception {
     stoChangeStorageDubboService.saveOrUpdate(stoChangeStorageEditVO);
     return ResponseUtil.ok("success");
    }

    @PostMapping("/del")
    @ApiOperation(value = "作废")
    public ApiResult del(@RequestBody @ApiParam(value = "主键id集合") List<String> list, @CurrentUser LoginInfo loginInfo) throws Exception {
     stoChangeStorageDubboService.delByList(list, "3", loginInfo.getUserCode());
     return ResponseUtil.ok("success");
    }

    @PostMapping("/reset")
    @ApiOperation(value = "恢复")
    public ApiResult reset(@RequestBody @ApiParam(value = "主键id集合") List<String> list, @CurrentUser LoginInfo loginInfo) throws Exception {
     stoChangeStorageDubboService.delByList(list, "0", loginInfo.getUserCode());
     return ResponseUtil.ok("success");
    }

    @GetMapping("/vinList")
    @ApiOperation(value = "获取车辆列表")
    @ApiImplicitParam(paramType = "query", name = "vin", value = "VIN码", required = false, dataType = "String")
    public ApiResult vinList(@CurrentUser LoginInfo loginInfo,String vin) throws Exception {
     Object partner = loginInfo.getPartner();
     PartnerVO partnerVO = new PartnerVO();
     if (partner != null) {
      BeanUtils.copyProperties(partnerVO, partner);
     }
     //预留字段1  存VIN
     if(StringUtils.isNotEmpty(vin)){
         partnerVO.setZobl1(vin);
     }
     return ResponseUtil.ok(stoChangeStorageDubboService.vinList(partnerVO));
    }

    @GetMapping("/storageList")
    @ApiOperation(value = "获取空闲库位")
    public ApiResult storageList(@CurrentUser LoginInfo loginInfo) throws Exception {
     return ResponseUtil.ok(stoChangeStorageDubboService.storageList());
    }

    @GetMapping("/pdaVehicleInfo")
    @ApiOperation(value = "PDA扫码获取车辆信息")
    @ApiImplicitParam(paramType = "query", name = "vin", value = "车辆VIN码", required = true, dataType = "String")
    public ApiResult pdaVehicleInfo(String vin) throws Exception {
     return ResponseUtil.ok(stoChangeStorageDubboService.pdaVehicleInfo(vin));
    }

    @PostMapping("/startChange")
    @ApiOperation(value = "开始移库")
    public ApiResult startChange(@RequestBody StoStockScanVO stoStockScanVO) throws Exception {
     stoChangeStorageDubboService.startChange(stoStockScanVO);
     return ResponseUtil.ok("success");
    }

    @PostMapping("/complete")
    @ApiOperation(value = "完成移库")
    public ApiResult complete(@RequestBody StoStockScanVO stoStockScanVO) throws Exception {
     stoChangeStorageDubboService.complete(stoStockScanVO);
     return ResponseUtil.ok("success");
    }

    @PostMapping("/import")
    @ApiOperation(value = "导入批量新增移库")
    public ApiResult importBatch(@RequestBody List<StoChangeStorageImportVO> list, @CurrentUser LoginInfo loginInfo) throws Exception {
     stoChangeStorageDubboService.importBatch(list);
     return ResponseUtil.ok("success");
    }

    @GetMapping("/planListByVin")
    @ApiOperation(value = "根据VIN码查询移库计划")
    @ApiImplicitParam(paramType = "query", name = "vin", value = "vin", required = true, dataType = "String")
    public ApiResult planListByVin(String vin) throws Exception {
     return ResponseUtil.ok(stoChangeStorageDubboService.planListByVin(vin));
    }

    @GetMapping("/export")
    @ApiOperation(value = "导出")
    public ApiResult exportList(StoChangeStorageQueryVO stoChangeStorageQueryVO) throws Exception {
     return ResponseUtil.ok(stoChangeStorageDubboService.exportList(stoChangeStorageQueryVO));
    }

}
